package com.sihai.ehr.service.impl;

import java.text.MessageFormat;

import javax.annotation.Resource;
import javax.persistence.Access;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Service;

import com.sihai.core.DataAccessor;
import com.sihai.core.DynamicQuery;
import com.sihai.core.Limit;
import com.sihai.core.LimitedList;
import com.sihai.core.QueryCreator;
import com.sihai.core.Sorter;
import com.sihai.ehr.model.biz.TrainMaterResource;
import com.sihai.ehr.model.biz.TrainRealiaResource;
import com.sihai.ehr.service.TrainMaterResourceService;

/**
 * 培训教育资源服务实现
 * 
 * @author henry
 * 
 */
@Service("trainMaterResourceService")
public class TrainMaterResourceServiceImpl implements TrainMaterResourceService {

	@Resource
	DataAccessor dataAccessor;

	@Override
	public TrainMaterResource getTrainMaterResource(Long id) {
		if (id == null || id < 0) {
			throw new RuntimeException("【传入参数非法】");
		}
		try {
			return dataAccessor.get(TrainMaterResource.class, id);
		} catch (Exception e) {
			throw new RuntimeException(
					"【异常原因：获取培训教育资源明细数据库发生异常】【异常说明：+"
							+ e.getMessage()
							+ "】【发生地点：TrainMaterResourceServiceImpl.getTrainMaterResource;参数id="
							+ id + " 】");
		}
	}

	@Override
	public LimitedList<TrainMaterResource> getTrainMaterResources(
			final Long organizationId, Long dutyId, Limit limit, Sorter sorter) {
		if (organizationId == null || organizationId < 0) {
			throw new RuntimeException("【传入参数非法】");
		}
		try {
			return dataAccessor.findByQuery(TrainMaterResource.class,
					new QueryCreator() {

						@Override
						public Query createQuery(Session session) {
							DynamicQuery dynamicQuery = new DynamicQuery(
									"from TrainMaterResource t where 1 <> 2");
							dynamicQuery
									.append("and t.trainPlan.organization.treePath like '%,"
											+ organizationId + ",%'");

							return dynamicQuery.build(session);
						}
					}, limit, sorter);
		} catch (Exception e) {
			throw new RuntimeException(
					"【异常原因：获取培训教育资源时数据库发生异常】【异常说明：+"
							+ e.getMessage()
							+ "】【发生地点：TrainMaterResourceServiceImpl.getTrainMaterResources;参数organizationId="
							+ organizationId + "】");
		}
	}

	@Override
	public void save(TrainMaterResource trainMaterResource) {
		if (trainMaterResource == null
				|| trainMaterResource.getTrainPlan() == null) {
			throw new RuntimeException("【lecturer对象参数异常】");
		}
		try {
			dataAccessor.saveOrUpdate(trainMaterResource);
		} catch (Exception e) {
			throw new RuntimeException("【异常原因：保培训教育资源对象到数据库发生异常】【异常说明："
					+ e.getMessage()
					+ "】【发生地点：TrainMaterResourceServiceImpl.save】");
		}
	}

	@Override
	public void deleteTrainMaterResource(final String ids) {
		if (ids == null || ids.isEmpty()) {
			throw new RuntimeException("【参数异常：ids为空】");
		}
		try {
			dataAccessor.batchUpdate(new QueryCreator() {
				@Override
				public Query createQuery(Session session) {
					return session.createQuery(MessageFormat
							.format("delete from TrainMaterResource t where t.id in ({0})",
									ids));
				}
			});
		} catch (Exception e) {
			throw new RuntimeException(
					"【异常原因：移出培训教育资源信息数据库发生异常】【异常说明："
							+ e.getMessage()
							+ "】【发生地点：TrainMaterResourceServiceImpl.deleteTrainMaterResource】");
		}
	}

	@Override
	public void deleteTrainMaterResourceByPlanId(final Long planId) {
		if (planId == null || planId < 0) {
			throw new RuntimeException("【参数异常：planId为空】");
		}
		try {
			dataAccessor.batchUpdate(new QueryCreator() {
				@Override
				public Query createQuery(Session session) {
					return session.createQuery(MessageFormat
							.format("delete from TrainMaterResource t where t.trainPlan.id={0}",
									planId));
				}
			});
		} catch (Exception e) {
			throw new RuntimeException(
					"【异常原因：移出培训教育资源信息数据库发生异常】【异常说明："
							+ e.getMessage()
							+ "】【发生地点：TrainMaterResourceServiceImpl.deleteTrainMaterResourceByPlanId】");
		}
	}

	@Override
	public void deleteTrainMaterResourceByPlanIds(final String ids) {
		if (ids == null || ids.isEmpty()) {
			throw new RuntimeException("【参数异常：ids为空】");
		}
		try {
			dataAccessor.batchUpdate(new QueryCreator() {
				@Override
				public Query createQuery(Session session) {
					return session.createQuery(MessageFormat
							.format("delete from TrainMaterResource t where t.trainPlan.id in ({0})",
									ids));
				}
			});
		} catch (Exception e) {
			throw new RuntimeException(
					"【异常原因：移出培训教育资源信息数据库发生异常】【异常说明："
							+ e.getMessage()
							+ "】【发生地点：TrainMaterResourceServiceImpl.deleteTrainMaterResourceByPlanIds】");
		}
	}

	@Override
	public LimitedList<TrainMaterResource> getTrainMaterResourcesByPlanId(
			final Long planId, Limit limit, Sorter sorter) {
		if (planId == null || planId < 0) {
			throw new RuntimeException("【planId传入参数非法】");
		}
		try {
			return dataAccessor.findByQuery(TrainMaterResource.class,
					new QueryCreator() {

						@Override
						public Query createQuery(Session session) {
							DynamicQuery dynamicQuery = new DynamicQuery(
									"from TrainMaterResource t where 1 <> 2");
							dynamicQuery.append("and t.trainPlan.id=" + planId);

							return dynamicQuery.build(session);
						}
					}, limit, sorter);
		} catch (Exception e) {
			throw new RuntimeException(
					"【异常原因：获取培训教育资源时数据库发生异常】【异常说明：+"
							+ e.getMessage()
							+ "】【发生地点：TrainMaterResourceServiceImpl.getTrainMaterResourcesByPlanId;参数planId="
							+ planId + "】");
		}
	}

}
